﻿using Outlook = Microsoft.Office.Interop.Outlook;
using Manager = OMC.DataManagement.Manager;
using System.Diagnostics;
using System;
using OMC.AddInHandling;

namespace OMC
{
    /// <summary>
    /// The main class contains the init, the start sequence and the destroy
    /// methods of the application. This class makes the application runnable.
    /// </summary>
    public partial class Main
    {
        private Manager manager { get; set; }
        private AddInHandler addInHandler { get; set; }

        /// <summary>
        /// This method initialize a manager and the handler of the application.
        /// </summary>
        /// <remarks>
        /// Source: http://msdn.microsoft.com/en-us/library/ms268750
        /// </remarks>
        private void Init()
        {
            try
            {
                Utils.Debug.Log("Initialising");

                //Initialize manager
                Outlook.Explorer explorer = Application.Explorers.Application.ActiveExplorer();
                Outlook.Stores stores = explorer.Session.Stores;
                Outlook.NameSpace nameSpace = explorer.Application.GetNamespace("MAPI");
                manager = new Manager(stores, nameSpace);
                addInHandler = new AddInHandler(stores, manager);

                //Initialize handler
                Application.ItemContextMenuDisplay
                    += new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(
                    addInHandler.contextMenuDisplay);
            }
            catch (Exception ex)
            {
                String message = "It isn't possible to init OMC Plugin";
                Utils.Debug.Log(message, ex, TraceEventType.Critical);
            }
        }

        /// <summary>
        /// This Method cleans up before the application goes down.
        /// </summary>
        private void CleanUp()
        {
            try
            {
                //remove Handler
                Application.ItemContextMenuDisplay
                    -= new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(
                    addInHandler.contextMenuDisplay);
                Utils.Debug.Log("Cleaned Up");
            }
            catch (Exception ex)
            {
                String message = "It isn't possible to clean Up OMC Plugin";
                Utils.Debug.Log(message, ex, TraceEventType.Critical);
            }
        }

        #region Startup,Shutdown
        private void main_Startup(object sender, System.EventArgs e)
        {
            Init();
        }

        private void main_Shutdown(object sender, System.EventArgs e)
        {
            CleanUp();
        }
        #endregion

        #region VSTO generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(main_Startup);
            this.Shutdown += new System.EventHandler(main_Shutdown);
        }
        #endregion
    }
}